/*
 * SimpleFilter.cpp
 *
 *  Created on: 2010-01-17
 *      Author: kamil
 */

#include "SimpleFilter.h"
#include <iostream>

using namespace std;

SimpleFilter::SimpleFilter(): NAME("SimpleFilter") {

}

SimpleFilter::~SimpleFilter() {

}

bool SimpleFilter::process(std::string &content)
{

	for(unsigned int i=0;i<oldNames.size();i++)
	{
		while(true)
		{
		size_t pos=0;
		size_t siz=0;
		pos=content.find(oldNames.at(i));
		if(pos==string::npos)
			break;
		siz=oldNames.at(i).length();

		content.replace(pos,siz,newNames.at(i));

		}
	}

	return true;
}

bool SimpleFilter::processConfig(std::string path)
{
	newNames.clear();
	oldNames.clear();
	ifstream configFile(path.c_str());
	    if(!configFile.is_open()){

	        return false;
	    }

	    string line;
	    while(getline(configFile, line)){
	    	int i=0;
	        if(line.size() == 0)
	            continue;
	        if(string::npos!=line.find_first_of("#"))
	            continue;
	        oldNames.push_back(line);

	        while(getline(configFile, line)){
	        	if(line.size() == 0)
	        	    continue;
	        	if(string::npos!=line.find_first_of("#"))
	        	    continue;
	        	newNames.push_back(line);

	        	i=1;
	        	break;
	        }
	        if(i==0) //brak jednego zamiennika
	        {

	        	return false;
	        }


	    }
	    configFile.close();

	return true;
}
std::string SimpleFilter::getName()
{
	return NAME;
}
